package com.stars.empoly.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import com.hwangjr.rxbus.annotation.Subscribe
import com.hwangjr.rxbus.annotation.Tag
import com.qmuiteam.qmui.kotlin.onClick
import com.stars.empoly.activity.MainActivity
import com.stars.empoly.adapter.HomeMechAdapter
import com.stars.empoly.adapter.HomeServerTypeAdapter
import com.stars.empoly.base.MBRFragment
import com.stars.empoly.config.ConfigRx
import com.stars.empoly.databinding.FragmentMechBinding
import com.stars.empoly.inte.ChoiceData
import com.stars.empoly.inte.ChoiceLocation
import com.stars.empoly.model.ShopLabel
import com.stars.empoly.utils.CurrentOneLocationUtils
import com.stars.empoly.utils.PopuBuyUtils
import com.stars.empoly.utils.PopuListUtils
import com.stars.empoly.vmodel.HomeMechVModel
import com.tencent.mmkv.MMKV

/**
 * 技师列表
 *on 2022/9/29
 */
class HomeMechFragemnt : MBRFragment<HomeMechVModel, HomeMechAdapter, FragmentMechBinding>() {


    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)


        //获取当前城市
        lifecycleScope.launchWhenCreated {
            CurrentOneLocationUtils.getStartLocal(context() as MainActivity, object :
                ChoiceLocation {
                override fun onRestuse(lat: String, lng: String, city: String) {
                    binding.tvCityName.text = city
                    MMKV.defaultMMKV().putString("city", city)
                    MMKV.defaultMMKV().putString("lat", lat)
                    MMKV.defaultMMKV().putString("lng", lng)
                }
            })
        }
        //获取城市的Id
        vm.getCityList()
//        var cityName=MMKV.defaultMMKV().getString("city", "")
        vm.cityList.observe(this) {
            if (it.isEmpty()) {
                return@observe
            }
            for (item in it) {
                if (item.name == "cityName") {
                    vm.cityId = item.id
                    //刷新
                    onRefresh()
                }
            }
        }


        //设置搜索条件
        var ss = ArrayList<ShopLabel>()
        ss.add(ShopLabel("0", "综合排序", true))//默认添加
        ss.add(ShopLabel("1", "距离", false))//默认添加
        ss.add(ShopLabel("2", "销量", false))//默认添加
        ss.add(ShopLabel("3", "好评度", false))//默认添加
        ss.add(ShopLabel("4", "热度", false))//默认添加


        var adapterType = HomeServerTypeAdapter()
        binding.recyclerType.adapter = adapterType
        adapterType.addData(ss)
        adapterType.setOnItemClickListener { adapterT, view, position ->

            var list = adapterT.data as ArrayList<ShopLabel>

            for ((index, value) in list!!.withIndex()) {
                if (index == position.toInt()) {

//                    vm.label_id=value.id
                    value.chose = true
                    adapterT?.notifyItemChanged(index)

                    adapter().clearList()
                    //刷新列表
                    onRefresh()
                } else {
                    if (value.chose) {
                        value.chose = false
                        adapterT?.notifyItemChanged(index)

                    }

                }

            }
        }







        vm.bean.observe(this) {
            //去下单

            PopuBuyUtils.show(context(), it, binding.cl)

        }

        vm.index.observe(this) {
            var bean = adapter().getItem(it)
            if (bean.isFollow == 0) {
                bean.isFollow = 1
            } else {
                bean.isFollow = 0
            }
            adapter().notifyItemChanged(it)
        }
    }

    override fun listeners() {
        super.listeners()
        binding.llCity.onClick {
            if (null != vm.cityList.value && vm.cityList.value!!.isNotEmpty()) {

                var list = java.util.ArrayList<String>()

                for (item in vm.cityList.value!!) {
                    list.add(item.name)
                }
                PopuListUtils.showQMUIPopup(context(), binding.llCity, list, object : ChoiceData {
                    override fun onRestuse(context: String?) {
                        for (item in vm.cityList.value!!) {
                            if (item.name == context) {
                                vm.cityId = item.id
                                binding.tvCityName.text = item.name
                                onRefresh()
                            }
                        }

                    }

                })


            }
        }
    }

    override fun vmClass(): Class<HomeMechVModel> {
        return HomeMechVModel::class.java
    }

    override fun binding(inflater: LayoutInflater, container: ViewGroup?): FragmentMechBinding {
        return FragmentMechBinding.inflate(layoutInflater)
    }

    override fun initAdapter(): HomeMechAdapter {
        return HomeMechAdapter(vm)
    }

    override fun title(): String? {
        return null
    }

    override val isRxbus: Boolean
        get() = true

    @Subscribe(tags = [Tag(ConfigRx.CANCEL_HEART_REFRESH)])
    fun setHeartRefresh(type: String) {
        //关注取消后刷新列表
        onRefresh()

    }
}
